// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    features: [
      {
        id: 'compress',
        name: '图片压缩',
        description: '高效压缩图片，减小体积',
        icon: 'compress'
      },
      {
        id: 'removebg',
        name: '抠图去背景',
        description: '智能抠图，一键去除背景',
        icon: 'cutout'
      },
      {
        id: 'recognize',
        name: '图片识别',
        description: '智能识别图片内容和文字',
        icon: 'scan'
      },
      {
        id: 'idphoto',
        name: '证件照制作',
        description: '快速制作各种规格证件照',
        icon: 'idcard'
      }
    ]
  },
  onLoad() {
    // 页面加载时执行
    console.log('图片处理小程序首页加载完成');
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  // 导航到功能页面
  navigateToFeature(e) {
    const feature = e.currentTarget.dataset.feature;
    console.log('正在导航到功能：', feature);
    
    // 根据功能ID导航到对应页面
    switch(feature) {
      case 'compress':
        wx.navigateTo({
          url: '../compress/compress',
          fail: err => {
            console.error('导航失败：', err);
            this.showFeatureNotReady();
          }
        });
        break;
      case 'removebg':
        wx.navigateTo({
          url: '../removebg/removebg',
          fail: err => {
            console.error('导航失败：', err);
            this.showFeatureNotReady();
          }
        });
        break;
      case 'recognize':
        wx.navigateTo({
          url: '../recognize/recognize',
          fail: err => {
            console.error('导航失败：', err);
            this.showFeatureNotReady();
          }
        });
        break;
      case 'idphoto':
        wx.navigateTo({
          url: '../idphoto/idphoto',
          fail: err => {
            console.error('导航失败：', err);
            this.showFeatureNotReady();
          }
        });
        break;
      default:
        console.error('未知功能：', feature);
        this.showFeatureNotReady();
    }
  },
  
  // 显示功能未准备好的提示
  showFeatureNotReady() {
    wx.showToast({
      title: '功能开发中，敬请期待',
      icon: 'none',
      duration: 2000
    });
  }
})
